package com.qezhhnjy.bilibili.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qezhhnjy.bilibili.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhaoyangfu - 2021/4/4 0:13
 */
@Slf4j
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "globalFallback")
public class OrderOpenFeignController {

    @Resource
    private PaymentService paymentService;

    @GetMapping("/payment/hystrix/ok")
    public String ok() {
        return paymentService.ok(15);
    }

    @GetMapping("/payment/hystrix/timeout")
    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2500")
    })
    public String timeout() throws Exception {
        // if (true) throw new Exception("11");
        // TimeUnit.SECONDS.sleep(3);
        return paymentService.timeout(12);
    }

    public String paymentInfoTimeoutHandler() {
        return "本地超时/异常，默认返回";
    }

    /**
     * 全局fallback
     */
    public String globalFallback() {
        return "global fallback...";
    }

}
